package com.martin.lib_base.net.interceptor

import com.martin.lib_base.constant.ConstantKey
import com.tencent.mmkv.MMKV
import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author：Houge
 * @date：2022/11/29
 * @desc：网络请求中添加固定Header
 */
class HeaderInterceptor : Interceptor {

    /**
     * 用户信息令牌
     */
    private val token: String?
        get() {
            return MMKV.defaultMMKV().decodeString(ConstantKey.TOKEN)
        }

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
            .newBuilder()
            // 添加用户信息Token
            .addHeader("Authorization", "Bearer $token")
            .build()

        return chain.proceed(request)
    }

}